<template>
	<view class="wrap">
		<u-form class="form" :model="model" :rules="rules" ref="uForm">
			<u-form-item label="服务器IP" prop="ip" label-width="180">
				<u-input type="" v-model="model.ip" placeholder="请输入"></u-input>
			</u-form-item>
			<u-form-item label="端口" prop="port" label-width="180">
				<u-input type="" v-model="model.port" placeholder="请输入"></u-input>
			</u-form-item>
			<u-form-item label="后缀" prop="suffix" label-width="180">
				<u-input type="" v-model="model.suffix" placeholder="请确认"></u-input>
			</u-form-item>
		</u-form>
		<view class="form-footer">
			<u-button class="btn" type="primary" @click="submit">设置服务器</u-button>
			<!-- <u-button class="btn" type="default" @click="cancel">关闭</u-button> -->
		</view>
	</view>
</template>
<script>
/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
import base64 from '@/common/base64.js';
export default {
	data() {
		return {
			model: {
				ip: '',
				port: '',
				suffix: ''
			},
			rules: {
				ip: [
					{
						required: true,
						message: '请输入服务器IP',
						trigger: ['change','blur'],
					}
				],
				port: [
					{
						required: true,
						message: '请输入端口',
						trigger: ['change','blur'],
					},
				],
				suffix: [
					{
						required: true,
						message: '请输入后缀',
						trigger: ['change','blur'],
					},
				],
			}
		};
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
		// http://192.168.64.101:8980/WMS

	},
	mounted() {
		const url = this.vuex_config.baseUrl;
		this.splitURL(url);
	},
	methods: {
		submit() {
				
			const baseUrlValue = this.model.ip+":"+this.model.port+"/"+this.model.suffix
			this.vuex_config.baseUrl = "http://"+ baseUrlValue;
			this.vuex_config.socketUrl = "ws://"+ baseUrlValue + '/socket'
			uni.setStorageSync('baseUrlValue', baseUrlValue);
			const regex = /^([^/]+)/;
			const match = baseUrlValue.match(regex);
			if (match && match[1]) {
			  // console.log(match[1]); // 输出：192.168.1.22:8980
			  this.vuex_config.xtUrl = "http://"+match[1]
			  this.$u.vuex('vuex_xtUrl', this.vuex_config.xtUrl);
			  this.$u.http.setConfig({ xtUrl: this.vuex_config.xtUrl });
			}
			this.$u.vuex('vuex_socketUrl', this.vuex_config.socketUrl);
			this.$u.http.setConfig({ socketUrl: this.vuex_config.socketUrl });
			this.$u.vuex('vuex_baseUrl', this.vuex_config.baseUrl);
			this.$u.http.setConfig({ baseUrl: this.vuex_config.baseUrl });
			this.$u.toast('设置成功');
			uni.navigateBack();
		},
		cancel() {
			uni.navigateBack();
		},
		splitURL(url) {
			const regex = /^http:\/\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})\/([A-Za-z0-9\-_]+)/;
			const match = url.match(regex);
			if (match) {
				this.model.ip = match[1]; // IP地址
				this.model.port = match[2]; // 端口号
				this.model.suffix = match[3]; // 路径或资源名
			} else {
				console.error('URL格式不匹配');
				return null;
			}
		},
	}
};
</script>
<style lang="scss">

</style>
